<template>
  <div class="registerbox">
    <el-card class="gray-card no-border no-margin-top" shadow="never">
      <el-form ref="form" :model="form" class="form">
        <el-row>
          <el-col :span="12">
            <el-form-item label="考生姓名：">
              <el-input v-model="form.userName" :disabled="disabled"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在科室：">
              <treeselect :disabled="disabled"
                          v-model="form.deptId"
                          :options="deptOptions" :show-count="true"
                          placeholder="请选择所在科室"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="考试科目：">
              <el-input v-model="form.subjects" :disabled="disabled"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考试名称：">
              <el-input v-model="form.testName" :disabled="disabled"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="考试总分：">
              <el-input v-model="form.totalScore" :disabled="disabled"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考试合格分数：">
              <el-input v-model="form.qualifiedScore" :disabled="disabled"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="考试时间：">
              <el-date-picker
                :disabled="disabled"
                v-model="form.testTime"
                type="datetime"
                placeholder="选择考试时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考试成绩：">
              <el-input v-model="form.score" :disabled="disabled"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="考试结果：">
              <el-input v-model="form.testResult" :disabled="disabled"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
    </el-card>
    <div class="details-foot">
      <div class="cell-btn">
        <el-button type="primary" @click.native="onSubmit" v-if="!disabled">立即创建</el-button>
        <el-button @click.native="handleClose">取消</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {treeselect} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {addTestRecords, updateTestRecords} from "@/api/business/testRecords"
import {parseTime} from '@/utils/ruoyi'

export default {
  name: "addExam",
  props: {
    disabled: {
      type: Boolean,
      default: false
    }
  },
  components: {Treeselect},
  data() {
    return {
      title: '新增',
      deptNode: null,
      deptOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      form: {
        userName: '',
        deptId: 100,
        subjects: '',
        testName: '',
        totalScore: '',
        qualifiedScore: '',
        testTime: '',
        testResult: '',
        dbStatus: 1
      }
    }
  },
  created() {
    this.getTreeselect();
  },
  methods: {
    onSubmit() {
      this.form.testTime = parseTime(this.form.testTime)
      if (this.form.id == null || this.form.id == undefined) {
        addTestRecords(this.form).then((res) => {
          this.$message.success('操作成功')
          this.handleClose()
        })
      } else {
        updateTestRecords(this.form).then((res) => {
          this.$message.success('操作成功')
          this.handleClose()
        })
      }
    },
    handleData(obj) {
      this.form.id = obj.id
      this.form.userName = obj.userName
      this.form.deptId = obj.deptId
      this.form.subjects = obj.subjects
      this.form.testName = obj.testName
      this.form.totalScore = obj.totalScore
      this.form.qualifiedScore = obj.qualifiedScore
      this.form.testTime = obj.testTime
      this.form.testResult = obj.testResult
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    handleClose() {
      this.form.userName = ''
      this.form.deptId = ''
      this.form.subjects = ''
      this.form.testName = ''
      this.form.totalScore = ''
      this.form.qualifiedScore = ''
      this.form.testTime = ''
      this.form.testResult = ''
      this.form.id = null
      this.$emit('close', false)
    }
  }
}
</script>

<style scoped>

</style>
